package com.dbq.fast.analysis;

import android.content.Context;
import android.os.Bundle;

import com.google.firebase.analytics.FirebaseAnalytics;

import java.math.BigDecimal;

/**
 * Created by sunkai on 2017/12/14.
 * <p>
 * 用户付费行为追踪
 */

public class FireBaseUserPurchaseTrack {

    private static FirebaseAnalytics mFirebaseAnalytics;

    private static FirebaseAnalytics getmFirebaseAnalytics(Context context) {
        if (mFirebaseAnalytics == null) {
            mFirebaseAnalytics = FirebaseAnalytics.getInstance(context.getApplicationContext());
        }
        return mFirebaseAnalytics;
    }


    /**
     * 登录事件
     *
     * @param context
     * @param channel 渠道
     */
    public static void login(Context context, String channel) {
        Bundle bundle = new Bundle();
        bundle.putString(FirebaseAnalytics.Param.SIGN_UP_METHOD, channel);
        getmFirebaseAnalytics(context).logEvent(FirebaseAnalytics.Event.LOGIN, bundle);

    }

    /**
     * 点击主页购买套餐
     *
     * @param context
     */
    public static void servicePage(Context context) {
        Bundle bundle = new Bundle();
//        bundle.putString(FirebaseAnalytics.Param.ITEM_CATEGORY, itemCategory);
        getmFirebaseAnalytics(context).logEvent(FirebaseAnalytics.Event.VIEW_ITEM_LIST, bundle);
    }


    /**
     *
     */
    /**
     * 添加到购物车（准备付款前）
     *
     * @param context
     * @param itemName 服务类型
     * @param itemId   服务id
     */
    public static void addToCard(Context context, String itemName, long itemId,String serviceEvent) {
        Bundle bundle = new Bundle();
        bundle.putLong(FirebaseAnalytics.Param.QUANTITY, 1);
//        bundle.putString(FirebaseAnalytics.Param.ITEM_CATEGORY, itemCategory);
        bundle.putString(FirebaseAnalytics.Param.ITEM_NAME, itemName);
        bundle.putString("service_name", serviceEvent);
        bundle.putLong(FirebaseAnalytics.Param.ITEM_ID, itemId);
//        bundle.putString(FirebaseAnalytics.Param.VALUE, value);
//        bundle.putString(FirebaseAnalytics.Param.PRICE, price);
//        bundle.putString(FirebaseAnalytics.Param.CURRENCY, currency);
//        bundle.putInt(FirebaseAnalytics.Param.ITEM_LOCATION_ID, itemLocationId);
        getmFirebaseAnalytics(context).logEvent(FirebaseAnalytics.Event.ADD_TO_CART, bundle);
    }

    /**
     * 点击支付
     *
     * @param context
     * @param value   价格(RMB)
     */
    public static void clickPlay(Context context, BigDecimal value,String  payMethod) {

        Bundle bundle = new Bundle();
        bundle.putString(FirebaseAnalytics.Param.CURRENCY, "CNY");
        bundle.putDouble(FirebaseAnalytics.Param.VALUE, value.doubleValue());
        bundle.putString("pay_method", payMethod);
        getmFirebaseAnalytics(context).logEvent(FirebaseAnalytics.Event.BEGIN_CHECKOUT, bundle);
    }


    /**
     * 支付成功
     *
     * @param value 货币金额(RMB)
     */
    public static void paySuccess(Context context, BigDecimal value,String payMethod) {
        Bundle bundle = new Bundle();
        bundle.putString(FirebaseAnalytics.Param.CURRENCY, "CNY");
        bundle.putDouble(FirebaseAnalytics.Param.VALUE, value.doubleValue());
        bundle.putString("pay_method", payMethod);
        getmFirebaseAnalytics(context).logEvent(FirebaseAnalytics.Event.ECOMMERCE_PURCHASE, bundle);
    }

}
